package com.woniu.aimd.controller;

import cn.dev33.satoken.util.SaResult;
import cn.hutool.json.JSONUtil;
import com.woniu.aimd.pojo.entity.CommentData;
import com.woniu.aimd.service.MergedCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/student-comments")
public class StudentCommentController {

    @Autowired
    private MergedCommentService mergedCommentService;

    /**
     * 保存或更新学生批注（整条覆盖）
     */
    @PostMapping ("/save")
    public SaResult save(@RequestBody Map<String, Object> body) {
        Map<String, Object> data = (Map<String, Object>) body.get("data");

        String noteId = (String) data.get("noteId");
        String userId = (String) data.get("userId");

        List<CommentData> commentList = JSONUtil.toList(
                JSONUtil.parseArray(data.get("comments")), CommentData.class
        );

        System.out.println(noteId);
        System.out.println(userId);
        mergedCommentService.saveComments(
                noteId,
                userId,
                commentList
        );
        return SaResult.ok("批注保存成功");
    }

    /**
     * 获取某学生某笔记的所有批注
     */
    @GetMapping("/get")
    public SaResult get(
            @RequestParam String noteId,
            @RequestParam String userId
    ) {
        List<CommentData> comments = mergedCommentService.loadComments(noteId, userId);
        return SaResult.data(comments);
    }
}